
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from common.Exception import BaseAPIException
from web.ppt import ppt  # 假设原来的 ppt 是 Flask 的 Blueprint，这里应转换为 FastAPI 的 Router


def create_app():
    app = FastAPI()

    # CORS 设置
    app.add_middleware(
        CORSMiddleware,
        allow_origins=["*"],  # 允许所有来源（可根据需要修改）
        allow_credentials=True,
        allow_methods=["*"],
        allow_headers=["*"],
    )

    # 注册路由
    app.include_router(ppt)  # ppt_router 需按 FastAPI 格式重写

    return app


app = create_app()

# 创建全局异常注册器
@app.exception_handler(BaseAPIException)
async def base_exception_handler(request: Request, exc: BaseAPIException):
    return JSONResponse(
        status_code=exc.status_code,
        content={
            "error_type": exc.error_type,
            "message": exc.detail
        }
    )
